/**
 * @description animation lib
 * @detail requestAnimationFrame
 * 1. duration
 * 2. from
 * 3. to
 * 4. type
 */
import Tween from './tween'

const customAnimation = exports.customAnimation = {};
customAnimation.to = function (duration, from, to, type, delay = 0) {
	console.log(duration, from, to, type)
  for (let prop in to) {
		setTimeout(function (prop) {
			return function () {
				console.log(to[prop])
				TweenAnimation(from[prop], to[prop], duration, type, (value, complete) => {
					from[prop] = value
				})
			}
		}(prop), delay * 1000)
	}
}

// 对运动方法进行封装
const TweenAnimation = exports.TweenAnimation = function TweenAnimation(from, to, duration, type, callback) {
	const frameCount = duration * 1000 / 17
	let start = -1

	const startTime = Date.now()
	let lastTime = Date.now()

	
	const options = {
		duration: 300,
		easing: 'Linear',
		callback: function callback() {}
	}
	if (callback) {
		options.callback = callback
	}

	if (type) {
		options.type = type
	}
	if (duration) {
		options.duration = duration
	}

	const tweenFn = Tween[type]

	const step = function step() {
		const currentTime = Date.now()
		const interval = currentTime - lastTime;
		let fps
		if (interval) {
			fps = Math.ceil(1000 / interval)
		} else {
			requestAnimationFrame(step)
			return;
		}
		if (fps >= 30) {
			start++;
		} else {
			const _start = Math.floor(interval / 17)
			start = start + _start
		}
		const value = tweenFn(start, from, to - from, frameCount)

		if (start <= frameCount) {
			// 动画继续
			options.callback(value)
			requestAnimationFrame(step)
		} else {
			// 动画结束
			options.callback(to, true)
		}
		lastTime = Date.now()
	}
	step()
}

TweenAnimation.killAll = function () {
}